package book.service;

import book.enums.BookStatus;
import book.mapper.BookMapper;
import book.model.BookInfo;
import book.model.PageRequest;
import book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理逻辑
 */
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    /**
     * 添加图书信息
     */
    public Integer addBook(BookInfo bookInfo){
        //数据校验
        if(bookInfo==null){
            return 0;
        }
        return bookMapper.addBook(bookInfo);
    }
    /**
     * 图书列表
     * @return
     */
    public PageResult<BookInfo> getBookListByPage(PageRequest request){
        //获取一页图书信息
        List<BookInfo> books = bookMapper.selectByPage(request.getOffset(),request.getPageSize());
//        对图书状态进行设置
        for (BookInfo book : books) {
//            book.setStatusCN(BookStatus.getPageStatusByCode(book.getStatus()).getDesc());
//            通过静态代码块+Map获取到状态描述
            book.setStatusCN(BookStatus.map.get(book.getStatus()));
        }
//        获取图书总条数
        int count=bookMapper.count();
//        返回结果集
        return new PageResult< >(books,count,request);
    }
    /**
     * 修改图书
     */
    public Integer updateBookById(BookInfo bookInfo){
        return bookMapper.updateBookById(bookInfo);
    }
    /**
     * 根据Id查询图书信息
     */
    public BookInfo selectBookById(Integer id){
        BookInfo bookInfos = bookMapper.selectBooKById(id);
//        设置图书状态描述:
        bookInfos.setStatusCN(BookStatus.map.get(bookInfos.getStatus()));
        return bookInfos;
    }
    /**
     * 物理删除图书信息
     */
    public Integer deleteBook(Integer id){
        BookInfo bookInfo=new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }
    /**
     * 批量删除图书信息
     */
    public Integer batchDelete(List<Integer> ids){
        return bookMapper.batchDelete(ids);
    }
}
